package com.example.onlineeducation.viewmodel


import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.onlineeducation.repository.UserRepository
import com.example.onlineeducation.base.BaseViewModel
import com.example.onlineeducation.data.entity.OfflineStudyLiveListEntity
import com.example.onlineeducation.data.entity.OfflineStudyReplaceSignListEntity
import com.example.onlineeducation.data.entity.SignInEntity
import com.example.onlineeducation.data.json.LoginJson
import com.example.onlineeducation.data.json.UploadFileResultJson
import com.example.onlineeducation.repository.OfflineRepository
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.tools.SdkVersionUtils
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.net.YcResult
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File


/**
 * Creator: yc
 * Date: 2021/6/15 18:03
 * UseDes:
 */
class OfflineStudySignBehalfVM : BaseViewModel() {

    private val mOfflineRepository: OfflineRepository by lazy { OfflineRepository(mApiService) }

    private val _mSendVerifyCode = MutableLiveData<YcResult<String>>()
    val mSendVerifyCode: LiveData<YcResult<String>> = _mSendVerifyCode

    /**
     * 发送短信验证码+
     * @param phoneNum String 手机号
     */
    fun sendVerifyCode(phoneNum: String) {
        ycLaunch {
//            mVerifyCodeSendLoop.start()
            mOfflineRepository.sendSignSmsCode(phoneNum).ycHasLoading(delayTime = 0)
                .collectLatest { result ->
                    _mSendVerifyCode.postValue(result)
                }
        }
    }

    /*
      * 文件上传
      * */
    private val _mUploadFileSignInResult = MutableLiveData<YcResult<UploadFileResultJson?>>()
    val mUploadFileSignInResult: LiveData<YcResult<UploadFileResultJson?>> =
        _mUploadFileSignInResult

    private val _mUploadFileSignOutResult = MutableLiveData<YcResult<UploadFileResultJson?>>()
    val mUploadFileSignOutResult: LiveData<YcResult<UploadFileResultJson?>> =
        _mUploadFileSignOutResult

    fun uploadFile(signInEntity: List<LocalMedia>, isSignIn: Boolean) = ycLaunch {
        val builder: MultipartBody.Builder = MultipartBody.Builder().setType(MultipartBody.FORM) //表单类型
        builder.addFormDataPart("biz", "offlineStudySignBehalf")//存储文件的目录，后台让我写死
        var file: File
        for (media in signInEntity) {
            file = if (media.compressPath.ycIsNotEmpty()) {
                File(if (SdkVersionUtils.checkedAndroid_Q()) media.androidQToPath else media.compressPath)
            } else {
                File(if (SdkVersionUtils.checkedAndroid_Q()) media.androidQToPath else media.path)
            }
            builder.addFormDataPart("file", file.name, file.asRequestBody("multipart/form-data".toMediaTypeOrNull())) //添加图片数据，body创建的请求体
        }
        val parts: List<MultipartBody.Part> = builder.build().parts
        mOfflineRepository.uploadFile(parts).ycHasLoading().collectLatest {
            if (isSignIn) {
                _mUploadFileSignInResult.postValue(it)
            } else {
                _mUploadFileSignOutResult.postValue(it)
            }

        }
    }

    private val _mReplaceSign = MutableLiveData<YcResult<String?>>()
    val mReplaceSign: LiveData<YcResult<String?>> = _mReplaceSign
    fun replaceSign(
        activeInfoId: String,
        address: String,
        lat: String,
        lng: String,
        reSignInMediaId: String,
        signInEntity: SignInEntity
    ) {
        ycLaunch {
            mOfflineRepository.replaceSign(
                activeInfoId,
                address,
                lat,
                lng,
                reSignInMediaId,
                signInEntity
            ).ycHasLoading(delayTime = 0).collectLatest { result ->
                _mReplaceSign.postValue(result)
            }
        }
    }


    private val _mReplaceOut = MutableLiveData<YcResult<String?>>()
    val mReplaceOut: LiveData<YcResult<String?>> = _mReplaceOut
    fun replaceSignOut(
        activeInfoId: String,
        address: String,
        lat: String,
        lng: String,
        reSignOutMediaId: String,
        replacePersonId: String
    ) {
        ycLaunch {
            mOfflineRepository.replaceSignOut(
                activeInfoId,
                address,
                lat,
                lng,
                reSignOutMediaId,
                replacePersonId
            ).ycHasLoading(delayTime = 0).collectLatest { result ->
                _mReplaceOut.postValue(result)
            }
        }
    }

    private val _mGetReplaceSignList =
        MutableLiveData<YcResult<List<OfflineStudyReplaceSignListEntity>?>>()
    val mGetReplaceSignList: LiveData<YcResult<List<OfflineStudyReplaceSignListEntity>?>> =
        _mGetReplaceSignList

    fun getReplaceSignList(proId: String) = ycLaunch {
        mOfflineRepository.getReplaceSignList(proId).ycHasLoading().collectLatest { result ->
            _mGetReplaceSignList.postValue(result)
        }
    }
}


